package com.cxs.videoplayer.slice;

import com.cxs.videoplayer.ResourceTable;
import com.cxs.videoplayer.controller.PlayerController;
import com.cxs.videoplayer.play.MPlayer;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.surfaceprovider.SurfaceProvider;
import ohos.agp.graphics.Surface;
import ohos.agp.graphics.SurfaceOps;
import ohos.agp.window.service.WindowManager;
import ohos.global.resource.RawFileDescriptor;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.common.Source;
import ohos.media.player.Player;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.util.*;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;

public class MainAbilitySlice extends AbilitySlice {
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00101, "MainAbility");
    private MPlayer mPlayer;
    private DirectionalLayout videoLayout;
    Text textInfo;
    Button btn_play, btn_pause;
    List<String> playList;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
//        textInfo = (Text)findComponentById(ResourceTable.Id_text_result);
        DirectionalLayout controllerLayout = (DirectionalLayout)findComponentById(ResourceTable.Id_controller_layout);

        videoLayout = (DirectionalLayout)findComponentById(ResourceTable.Id_video_layout);
        btn_play = (Button) findComponentById(ResourceTable.Id_btn_play);

        String url = "resources/rawfile/gubeishuizhen.mp4";
        String url1 ="resources/rawfile/movie.mp4";

        playList = new ArrayList<>();
        playList.add(url);
        playList.add(url1);

        MPlayer.Builder builder = new MPlayer.Builder(this);
        MPlayer mPlayer = builder.setFilePath(url).setStartMilliSecond(0).build();

        videoLayout.addComponent(mPlayer.getSurfaceView());

        PlayerController playerController = new PlayerController(this, mPlayer, playList);
        // 把PlayerController里的statusChangeListener实例传给mPlayer
        mPlayer.addStatusCallback(playerController.statusChangeListener);
        mPlayer.open();

        controllerLayout.addComponent(playerController);
    }



    private void showInfo(String s) {
        getUITaskDispatcher().asyncDispatch(new Runnable() {
            @Override
            public void run() {
                textInfo.append(s + "\n");
            }
        });
    }
    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
